package com.freshcity.component.container
{
	import com.freshcity.IDragDrop;
	import com.freshcity.ILayer;
	import com.freshcity.common.CloseIcon;
	import com.freshcity.graphics.WinBackground;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TweenLite;
	import com.greensock.easing.Elastic;
	
	import fl.controls.Label;
	
	import flash.display.Bitmap;
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Rectangle;
	import flash.text.TextField;


	[Event(name="select", type="flash.events.Event")]
	[Event(name="close", type="flash.events.Event")]
	[Event(name="resize", type="flash.events.Event")]

	public class Window extends Component
	{
		private var _layer:ILayer;

		protected var _title:String;
		protected var _titleBar:Sprite;
		protected var _titleLabel:TextField;
		protected var _panel:Component;
		protected var _color:int=-1;
		protected var _shadow:Boolean=true;
		protected var _draggable:Boolean=true;
		protected var _hasCloseButton:Boolean=false;
		protected var _isBitmapTitle:Boolean=false;

		protected var _back:WinBackground;
		private var _stagerect:Rectangle;
		protected var _titleHeight:int=30;
		private var _closeButton:SimpleButton;

		/**
		 * Constructor
		 * @param parent The parent DisplayObjectContainer on which to add this Panel.
		 * @param xpos The x position to place this component.
		 * @param ypos The y position to place this component.
		 * @param title The string to display in the title bar.
		 */
		public function Window(parent:ILayer=null, xpos:Number=0, ypos:Number=0, title:String="Window")
		{
			_title=title;
			this._stagerect=new Rectangle();
			super(_layer as DisplayObjectContainer, xpos, ypos);
			addEventListener(MouseEvent.MOUSE_DOWN, this.onWindowDownHandler);
		}

		protected function onWindowDownHandler(event:MouseEvent):void
		{
			this.layer.setTop(this);
		}

		/**
		 * Initializes the component.
		 */
		override protected function init():void
		{
			super.init();
			setSize(100, 100);
		}

		/**
		 * Creates and adds the child display objects of this component.
		 */
		override protected function addChildren():void
		{
			_back=new WinBackground();
			//_back.filters=[getShadow(2, true)];
			super.addChild(_back);

			_titleBar=new Sprite();
			_titleBar.height=_titleHeight;
			_titleBar.filters=[];
			_titleBar.buttonMode=true;
			//_titleBar.useHandCursor=true;
			_titleBar.addEventListener(MouseEvent.MOUSE_DOWN, onMouseGoDown);
			super.addChild(_titleBar);
			
			_titleLabel= StringUtil.getTextField("");
			_titleLabel.filters = [new GlowFilter(0,1,5,5,1)];
			super.addChild(_titleLabel);
			
			_closeButton= new CloseIcon();
			_closeButton.width=12;
			_closeButton.height=12;
			_closeButton.addEventListener(MouseEvent.CLICK,hide);

			_panel=new Component(null, 0, 34);
			//_panel.mouseEnabled=false;
			super.addChild(_panel);

			//_helpButton=new PushButton(null,64,4,"",onhelp);
			//_closeButton=_back.closeBtn;

			//this._popupSprite = new Sprite();
			//filters=[getShadow(4, false)];
		}

		///////////////////////////////////
		// public methods
		///////////////////////////////////

		/**
		 * Overridden to add new child to content.
		 */
		public override function addChild(child:DisplayObject):DisplayObject
		{
			content.addChild(child);
			return child;
		}

		/**
		 * Draws the visual ui of the component.
		 */
		override public function draw():void
		{
			super.draw();
			//this.visible = false;
			if (x == 0)
			{
				x=Math.round(stage.stageWidth / 2 - width / 2);
			}
			if (y == 0)
			{
				y=Math.round(stage.stageHeight / 2 - height / 2);
			}

			_back.width=width;
			_back.height=height;

			_titleBar.graphics.clear();
			_titleBar.graphics.beginFill(0x00cccc, 0.01);
			_titleBar.graphics.drawRect(0, 0, width, _titleHeight);
			_titleBar.graphics.endFill();
			_titleBar.width=width;
			_titleBar.height=_titleHeight;

			_titleLabel.x=10;
			_titleLabel.y=5;
			_titleLabel.htmlText =  StringUtil.getHtmlStyleText(_title, "#FFFFFF", 16, "楷体");

			//_helpButton.x=_width-45;
			_closeButton.x=_width - 24;
			_closeButton.y=10;
			_panel.setSize(_width, _height - 40);
		}

		override public function show(event:Event=null):void
		{
			//this.visible = true;			
			//var tx:int=Math.round(stage.stageWidth / 2 - width / 2);
			//var ty:int=Math.round(stage.stageHeight / 2 - height / 2);
			this.layer.setTop(this);
			TweenLite.from(this, 0.3, {alpha: 0});
		}

		override public function hide(event:Event=null):void
		{
			var xv:Number=this.x + this.width / 2;
			var vy:Number=this.y + this.height;
			//TweenLite.to(this, 1, {alpha:0, x:xv, y:vy, scaleX:0, scaleY:0,onComplete:onTweenComplete});	
			TweenLite.to(this, 0.3, {alpha: 0, onComplete: destroy});
		}

		///////////////////////////////////
		// event handlers
		///////////////////////////////////

		/**
		 * Internal mouseDown handler. Starts a drag.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onMouseGoDown(event:MouseEvent):void
		{
			if (event.target != this._titleBar)
				return;
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseGoUp);
			this._stagerect.x=0;
			this._stagerect.y=0;
			this._stagerect.width=this.stage.stageWidth - this._width;
			this._stagerect.height=this.stage.stageHeight - this._height;
			this.startDrag(false, this._stagerect);

			dispatchEvent(new Event(Event.SELECT));
		}

		/**
		 * Internal mouseUp handler. Stops the drag.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onMouseGoUp(event:MouseEvent):void
		{
			this.stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseGoUp);
		}

		protected function onhelp(event:MouseEvent):void
		{
			//TweenLite.to(this, 1, {y:stage.height, ease:Bounce.easeIn});
			//this.visible = false;
			//			this.hide();
			//			this.parent.removeChild(this);
		}

		protected function onClose(event:MouseEvent):void
		{
			//TweenLite.to(this, 1, {y:stage.height, ease:Bounce.easeIn});
			//this.visible = false;
			this.hide();
			//this.parent.removeChild(this);
		}

		public function popup(event:MouseEvent):void
		{
			//var timeline:TimelineLite = new TimelineLite();
			//timeline.append( new TweenLite(this, 1, {alpha:1,height:20,width:20}) );
			//TweenLite.to(this, 1, {y:stage.height/2, ease:Bounce.easeOut});
			this.visible=true;
		}

		///////////////////////////////////
		// getter/setters
		///////////////////////////////////

		/**
		 * Gets / sets whether or not this Window will have a drop shadow.
		 */
		public function set shadow(b:Boolean):void
		{
			_shadow=b;
			if (_shadow)
			{
				filters=[getShadow(4, false)];
			}
			else
			{
				filters=[];
			}
		}

		public function get shadow():Boolean
		{
			return _shadow;
		}

		/**
		 * Gets / sets the background color of this panel.
		 */
		public function set color(c:int):void
		{
			_color=c;
			invalidate();
		}

		public function get color():int
		{
			return _color;
		}

		/**
		 * Gets / sets the title shown in the title bar.
		 */
		public function set title(t:String):void
		{
			_title=t;
			this.invalidate();
			//			_titleLabel.text=_title;
		}

		public function get title():String
		{
			return _title;
		}

		/**
		 * Container for content added to this panel. This is just a reference to the content of the internal Panel, which is masked, so best to add children to content, rather than directly to the window.
		 */
		public function get content():DisplayObjectContainer
		{
			return _panel;
		}

		/**
		 * Sets / gets whether or not the window will be draggable by the title bar.
		 */
		public function set draggable(b:Boolean):void
		{
			_draggable=b;
			_titleBar.buttonMode=_draggable;
			_titleBar.useHandCursor=_draggable;
		}

		public function get draggable():Boolean
		{
			return _draggable;
		}

		/**
		 * Gets the height of the component. A minimized window's height will only be that of its title bar.
		 */
		override public function get height():Number
		{
			if (contains(_panel))
			{
				return super.height;
			}
			else
			{
				return 20;
			}
		}

		/**
		 * Sets / gets whether or not the window will display a close button.
		 * Close button merely dispatches a CLOSE event when clicked. It is up to the developer to handle this event.
		 */
		public function set hasCloseButton(value:Boolean):void
		{
			_hasCloseButton=value;
			if (_hasCloseButton)
			{
				_titleBar.addChild(_closeButton);

			}
			else if (_titleBar.contains(_closeButton))
			{
				_titleBar.removeChild(_closeButton);

			}
			invalidate();
		}

		public function get hasCloseButton():Boolean
		{
			return _hasCloseButton;
		}

		/**
		 * Returns a reference to the title bar for customization.
		 */
		public function get titleBar():Sprite
		{
			return _titleBar;
		}

		public function set titleBar(value:Sprite):void
		{
			_titleBar=value;
		}

		public function get layer():ILayer
		{
			return _layer;
		}

		public function set layer(value:ILayer):void
		{
			_layer=value;
		}

	}
}
